package cn.chenyumeng.blog.service.blog.impl;

import cn.chenyumeng.blog.dto.BlogDTO;
import cn.chenyumeng.blog.entity.Blog;
import cn.chenyumeng.blog.entity.BlogTag;
import cn.chenyumeng.blog.mapper.BlogMapper;
import cn.chenyumeng.blog.mapper.BlogTagMapper;
import cn.chenyumeng.blog.mapper.CommentMapper;
import cn.chenyumeng.blog.service.blog.HomePageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: blog->HomePageServiceImpl
 * @description:
 * @author: cg
 * @create: 2020-08-10 19:40
 **/
@Service
public class HomePageServiceImpl implements HomePageService {

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private BlogTagMapper blogTagMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public PageInfo<BlogDTO> queryAllByLimit(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Blog> blogs = blogMapper.queryAllBlogListByLatestPublished();
        if (blogs.isEmpty()) {
            return null;
        }
        PageInfo blogPageInfo = new PageInfo(blogs);
        List<BlogDTO> dtoList = new ArrayList<>();
        blogPageInfo.getList().forEach(blog -> {
            BlogDTO blogDTO = new BlogDTO();
            BeanUtils.copyProperties(blog, blogDTO);
            blogDTO.setTagIds(getTagIds(blogDTO.getId()));
            blogDTO.setCommentCount(commentMapper.queryBlogCommentCountByBlogId(blogDTO.getId()));
            dtoList.add(blogDTO);
        });
        blogPageInfo.setList(dtoList);
        return blogPageInfo;
    }

    @Override
    public List<BlogDTO> queryBlogLatestRecommend(Integer start, Integer end) {
        List<Blog> blogs = blogMapper.queryLatestRecommend(start, end);
        if (blogs.isEmpty()) {
            return null;
        }
        List<BlogDTO> list = new ArrayList<>();
        blogs.forEach(blog -> {
            BlogDTO dto = new BlogDTO();
            BeanUtils.copyProperties(blog,  dto);
            list.add(dto);
        });
        return list;
    }

    @Override
    public PageInfo<BlogDTO> search(String content, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Blog> blogs = blogMapper.queryBlogListByLatestPublishedLikeTitleAndContent(content);
        if (blogs.isEmpty()) {
            return null;
        }
        PageInfo blogPageInfo = new PageInfo(blogs);
        List<BlogDTO> dtoList = new ArrayList<>();
        blogPageInfo.getList().forEach(blog -> {
            BlogDTO blogDTO = new BlogDTO();
            BeanUtils.copyProperties(blog, blogDTO);
            blogDTO.setTagIds(getTagIds(blogDTO.getId()));
            blogDTO.setCommentCount(commentMapper.queryBlogCommentCountByBlogId(blogDTO.getId()));
            dtoList.add(blogDTO);
        });
        blogPageInfo.setList(dtoList);
        return blogPageInfo;
    }

    /**
     * 通过tagIds查询所对应的blogIds
     * @param tagIds
     * @return
     */
    private List<Long> getBlogIds(List<Long> tagIds) {
        // 用来存储标签Id集所对应的博客Id集
        List<Long> blogIds = new ArrayList<>();
        // 通过标签id集获取对应的博客标签集
        List<BlogTag> blogTags = blogTagMapper.selectBlogTagsByTagIds(tagIds);
        // 循环将博客标签集的每个博客id存入博客Id集
        blogTags.forEach(blogTag -> blogIds.add(blogTag.getBlogId()));
        return blogIds;
    }

    /**
     * 通过blogId查询所对应的tagIds
     * @param blogId
     * @return
     */
    private List<Long> getTagIds(Long blogId) {
        List<BlogTag> blogTags = blogTagMapper.selectBlogTagsByBlogId(blogId);
        List<Long> tags = new ArrayList<>();
        blogTags.forEach(blogTag -> tags.add(blogTag.getTagId()));
        return tags;
    }
}
